##########################################
## "Voters get what they want"          ##
##########################################
## Heinrich, Kobayashi, Long            ##
## International Studies Quarterly      ##
##########################################

## Main file that executes the analysis 
## June 29, 2017

## Note: The code to merge does not do the data wrangling based on either 
## Nielsen's replication or the CCES data as the source files are quite big. 
## The code is there for reference. If the original files are sought,
## please contact Tobias Heinrich (heinrict@mailbox.sc.edu)


## Set seed for analysis
########################
set.seed(6292017)


## Preliminaries
################
rm(list=ls())
setwd("/Users/th5/Dropbox/HeinrichKobayashiLong2017/")

library(foreign)
library(MCMCpack)
library(reshape2)
library(MCMCglmm)
library(foreach)
library(doMC)
library(plyr)
library(stringr)
library(matrixStats)
library(stargazer)
library(countrycode)
library(ggplot2)
library(AlgDesign)
library(lme4)
source("code/x_Auxiliary functions.R")

## Create folders that hold outputs
dir.create(path="output", showWarnings = FALSE)
dir.create(path="output/analysis1_subset", showWarnings = FALSE)
dir.create(path="output/analysis1_interaction", showWarnings = FALSE)
dir.create(path="output/analysis2", showWarnings = FALSE)
dir.create(path="output/tables", showWarnings = FALSE)
dir.create(path="output/figures", showWarnings = FALSE)
dir.create(path="output/InArticle", showWarnings = FALSE)

## Make holder for Gelman-Rubin-Brooks statistics
Rhat <- data.frame(Rhat=NA, Which=NA)
save(x=Rhat, file="output/Rhat.Rdata")

## MCMC settings show all models in the analysis
niter <- 100000
burnin <- round(niter/2, di=0)
chains <- 3
thin <- 10

## Settings for parallel
registerDoMC(cores=2)


## Analysis 1 (TSCS)
####################
## Prep Nielsen/Lake data
## The files are copied from "data" to the output; executing
## the code below (that's commented out) would have produced 
## these files in the output folder.
#source("code/01_Prep Nielsen data.R")
file.copy(from="data/data_EH.Rdata",
          to="output/data_EH.Rdata", overwrite=TRUE)
file.copy(from="data/data_SH.Rdata",
          to="output/data_SH.Rdata", overwrite=TRUE)


## Re-analyze Nielsen
source("code/02_Re-analysis of Nielsen.R")

## Estimate subset models with hierarchy
source("code/03_Estimate subset models.R")

## Substantive effects of subset models
source("code/04_Effects of subset models.R")

## Make tables of subset models
source("code/05_Make tables of subset models.R")

## Estimate interaction models
source("code/06_Estimate interaction models.R")

## Substantive effects of interaction models
source("code/07_Effects of interaction models.R")

## Marginal effects of marginal effects
source("code/08_Probabilities of support.R")

## Descriptives for Nielsen data
source("code/09_Descriptives of Nielsen data.R")


## Analysis 2 (Survey experiments)
##################################
## Draws of the MCMC output used for marginalization
approx <- min(5000, niter-burnin-1)

## Prep CCES data
## To save file space again, we're copying a file that
## is the output of this script to the destination.
#source("code/10_Prep CCES data.R")
file.copy(from="data/data_preppedCCES.Rdata",
          to="output/data_preppedCCES.Rdata", overwrite=TRUE)

## Analyze September experiment
source("code/11_Estimates of September survey experiment.R")

## Analyze January experiment
source("code/12_Estimates of January survey experiment.R")

## Descriptives
source("code/13_Descriptives of survey experiments.R")


## Assess convergences
######################
source("code/14_Assess convergence.R")


## Copy files to names used in article
######################################
file.copy(from="output/tables/a2_rr_september.tex",
          to="output/InArticle/Table_1.tex", overwrite=TRUE)
file.copy(from="output/figures/A2-September-NoCovs.pdf",
          to="output/InArticle/Figure_1.pdf", overwrite=TRUE)
file.copy(from="output/tables/table_a1_subset_NYTleader_short.tex",
          to="output/InArticle/Table_2.tex", overwrite=TRUE)
file.copy(from="output/figures/A1-ME-Subset-Top40-Leader.pdf",
          to="output/InArticle/Figure_2.pdf", overwrite=TRUE)
file.copy(from="output/figures/A1-ME-Subset-Lowest40-Leader.pdf",
          to="output/InArticle/Figure_3.pdf", overwrite=TRUE)
file.copy(from="output/figures/A1-Probabilities-Main.pdf",
          to="output/InArticle/Figure_4.pdf", overwrite=TRUE)
